package com.mvp.shq.washer.ui.forgetPWD;

import com.mvp.shq.washer.entity.BaseResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.SignUtils;
import com.mvp.shq.washer.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/24.
 */

public class ForgetPWDPresenter extends ForgetPWDConstract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    void forget(String mobile, String code, String pwd) {
        String timestamp = SignUtils.getTime();
        pwd = StringUtils.MD5(pwd);
        Map<String,String> map = new HashMap();
        map.put("mobile",mobile);
        map.put("pwd",pwd);
        map.put("timestamp",timestamp);
        map.put("code",code);
        String sign = SignUtils.getSignature(map);
        model.forget(mobile,code,pwd,timestamp,sign)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (!t.isError()){
                            view.rSuccess();
                        }else {
                            view.rFial(t.getMsg());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        view.rFial("未知错误");
                    }
                });
    }

    @Override
    void code(String mobile) {
        model.code(mobile)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (!t.isError()){
                            view.codeSuccess();
                        }else {
                            view.codeFail(t.getMsg());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        view.codeFail("获取失败:未知失败");
                    }
                });
    }
}
